<?php
namespace Controller;

require_once('PostHandler.php');
require_once('PostView.php');
require_once("Moduler/Login/LoginHandler.php");

class PostController{
	
	private $errorArray = array();
	
	//antagligen den mest krångliga och ostrukturerade metoden i projektet.
	//TODO: strukturera koden lite här.
	
	public function DoPostControll($db){
		$ph = new \PostHandler($db);
		$pv = new \View\PostView();
		$lh = new \LoginHandler($db);
		
		$userrights = \View\PostView::READRIGHTS;
		
		$user = $lh->IsLoggedIn(true);
		
		if($user != false){
			$userrights = \View\PostView::COMMENTRIGHTS;
			if($user->getUserType() == '1'){
				$userrights = \View\PostView::ADMINRIGHTS;
			}
		}
		
		$html = '';
		$tags;
		
		$posts;
		
		if($pv->TriedToComment()){ //lägga till kommentar
			$ph->addComment($pv->getPostId(), $pv->getCommentText(), $user->getUserId());
		}
		
		//TODO: felmeddelnade
		//ta bort kommentar
		if(($commentId = $pv->TriedToRemoveComment()) != false){
			if($userrights == \View\PostView::ADMINRIGHTS){
				$ph->removePostOrComment($commentId, 'postcomment');
				$pv->RemoveGetVar('removeCommentId');
			}
		}
		//TODO: felmeddelnade
		//ta bort post
		if(($PostId = $pv->TriedToRemovePost()) != false){
			if($userrights == \View\PostView::ADMINRIGHTS){
				$ph->removePostOrComment($PostId, 'post');
				$pv->RemoveGetVar('removePostId');
			}
		}
		
		if($pv->TriedToMakePost()){ 
			//lite validering
			if(!$ph->titleLength($pv->getNewPostTitle())){
				array_push($this->errorArray, \PostHandler::TITLELENGTH);
			}
			
			if(!$ph->textLength($pv->getNewPostText())){
				array_push($this->errorArray, \PostHandler::TEXTLENGTH);
			}
			
			//om de inte finns några felmeddelanden
			if(sizeof($this->errorArray) == 0){
				if($pv->TriedToUploadFile()){
					$fileSrc = $ph->saveFile($pv->getFile()); //sparar filen, returnerar bildens sökväg om det går vägen, annars returneras en array med felmeddelanden
					
					if(is_array($fileSrc)){
						$this->errorArray = array_merge($this->errorArray, $fileSrc);
					}else{
						$ph->CreateNewPost($pv->getNewPostTitle(), $pv->getNewPostText(), $pv->getPostTags(), $fileSrc);
					}
				}else{
					$ph->CreateNewPost($pv->getNewPostTitle(), $pv->getNewPostText(), $pv->getPostTags(), NULL);
				}
			}
		}
		
		if($userrights == \View\PostView::ADMINRIGHTS){
			$html .= $pv->DoMakePostButtons();
			$html .= $pv->GenerateNewPostBox();
			$html .= $pv->GenerateNewPictureBox();
		}
		
		if($pv->getPostViewTags() != false){
			$posts = $ph->getPostByTag($pv->getPostViewTags());
		}else{
			$posts =  $ph->getPost();
		}
		
		
		foreach($posts as $p){
			$html .= $pv->generatePost($p);
			
		
				$html .= $pv->generateCommentLine($p, $userrights);
				
				foreach($p->postComments() as $c){
					$html .= $pv->generateComments($c, $userrights);
				}
				
				$html .= $pv->generateDoCommentForm($p->postId());
		
			
			$html .= $pv->generatePostEnd();
		}
		$errorsHTML='';
		if(sizeof($this->errorArray) != 0){
			$errorsHTML = $pv->addErrors($this->errorArray);
		}

		
		return $errorsHTML.$html;
	}
}

?>